package Servlet;

import DAO.CustomerDAO;
import DAO.DAOFactory;
import javabean.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/ResetPassword")
public class ResetPasswordServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String phone = request.getParameter("phone");
        String newpassword = request.getParameter("newpassword");
        //封装成javabean
        CustomerDAO customerDAO = (CustomerDAO) DAOFactory.getCustomerDAOInstance();
        Customer customer = new Customer(username, null, phone, null, 0, null);
        try {
            if (customerDAO.ResetPassword(customer, newpassword)) {
                //request.setAttribute("message","success");
                request.getRequestDispatcher("Login.jsp").forward(request,response);
            }else{
                //request.setAttribute("message","fail");
                request.getRequestDispatcher("Retrieve_password.jsp").forward(request,response);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
